﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

[HubName("TracNghiem")]
public class TracNghiem : Hub
{
    public static int count = 1;
    public static DateTime currentDate = DateTime.Now.Date;

    public static List<ClientUser> ListClient = new List<ClientUser>();
    public static List<ClientUser> ListAdmin = new List<ClientUser>();

    public void addUserServer(ClientUser obj)
    {
        foreach (var item in ListAdmin)
        {
            var clientIDs = item.Client_session.Split('$');
            for (int i = 0; i < clientIDs.Count() - 1; i++)
            {
                Clients.Client(clientIDs[i]).addUserServer(obj);
            }
        }
    }
    public void removeUserServer(string id)
    {
        foreach (var item in ListAdmin)
        {
            var clientIDs = item.Client_session.Split('$');
            for (int i = 0; i < clientIDs.Count() - 1; i++)
            {
                Clients.Client(clientIDs[i]).removeUserServer(id);
            }
        }
    }
 
    public void updateTime(int code)
    {
        BaiThiDAL.UpdateTime(code,DateTime.Now);
    }

    public override System.Threading.Tasks.Task OnConnected()
    {
        string session = Context.Request.Cookies["ID"].ToString();
        string name = Context.Request.Cookies["Name"].ToString();
        int type = 1;
        if (Context.QueryString["t"] != null && !Context.QueryString["t"].ToString().Equals(""))
            type = int.Parse(Context.QueryString["t"].ToString());
        var fg = false;
        if (type == 1)
        {
            foreach (var item in ListClient)
            {
                if (item.Asp_session.IndexOf(session) > -1)
                {
                    item.Client_session += Context.ConnectionId + "$";
                    fg = true;
                    break;
                }
            }
        }
        else
        {
            int mahv = int.Parse(Context.Request.Cookies["bidanh"].ToString());
            HocVienINFO nguodung = HocVienDAL.GetInfo(mahv);
            foreach (var item in ListAdmin)
            {
                if (item.Asp_session.IndexOf(session) > -1)
                {
                    item.Client_session += Context.ConnectionId + "$";
                    fg = true;
                    break;
                }
            }
        }
        if (!fg)
        {
            ClientUser client = new ClientUser();
            client.Asp_session = session;
            client.Client_session = Context.ConnectionId + "$";
            client.Type = type;
            client.Name = name;
            if (type == 1)
            {
                ListClient.Add(client);
                addUserServer(client);
            }
            else
            {
                ListAdmin.Add(client);
                Clients.All.getOnline();
            }
            count++;
        }
        return base.OnConnected();
    }

    public override System.Threading.Tasks.Task OnReconnected()
    {
        Clients.All.reconnected();
        return base.OnReconnected();
    }

    public override System.Threading.Tasks.Task OnDisconnected()
    {
        foreach (var item in ListClient)
        {
            if (item.Client_session.IndexOf(Context.ConnectionId) > -1)
            {
                item.Client_session = item.Client_session.Replace(Context.ConnectionId + "$", "");
                if (item.Client_session.Equals(""))
                {
                    ListClient.Remove(item);
                    removeUserServer(item.Asp_session);
                }
                break;
            }
        }
        foreach (var item in ListAdmin)
        {
            if (item.Client_session.IndexOf(Context.ConnectionId) > -1)
            {
                item.Client_session = item.Client_session.Replace(Context.ConnectionId + "$", "");
                if (item.Client_session.Equals(""))
                {
                    ListAdmin.Remove(item);
                }
                break;
            }
        }
        return base.OnDisconnected();
    }
}